package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.wemedia.entity.WmNewsMaterial;
import com.heima.wemedia.mapper.WmNewsMaterialMapper;
import com.heima.wemedia.service.WmNewsMaterialService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.lang.invoke.LambdaConversionException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 自媒体图文引用素材信息表 服务实现类
 * </p>
 *
 * @author HM
 * @since 2023-02-15
 */
@Service
public class WmNewsMaterialServiceImpl extends ServiceImpl<WmNewsMaterialMapper, WmNewsMaterial> implements WmNewsMaterialService {

    /**
     * 根据文章id删除素材中间表数据
     * @param wmNewsId
     */
    @Override
    public void removeByNewsId(Integer wmNewsId) {
        LambdaQueryWrapper<WmNewsMaterial> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmNewsMaterial::getNewsId,wmNewsId);
        remove(queryWrapper);
    }

    /**
     * 批量保存 文章素材中间表数据
     * @param materialIds
     * @param type 0-内容素材 1-封面素材
     */
    @Override
    public void saveNewsAndMaterialBatch(List<Integer> materialIds, Integer wmNewsId, int type) {

        List<WmNewsMaterial> list = new ArrayList<>();
        for (Integer materialId : materialIds) {
            WmNewsMaterial wmNewsMaterial = new WmNewsMaterial();
            wmNewsMaterial.setNewsId(wmNewsId);
            wmNewsMaterial.setMaterialId(materialId);
            wmNewsMaterial.setType(type);

            list.add(wmNewsMaterial);
        }
        saveBatch(list);

    }
}

